package com.ylcy.ads.utils;

import android.annotation.SuppressLint;
import android.app.KeyguardManager;
import android.content.Context;
import android.os.PowerManager;

import com.ylcy.ads.config.ENV;

import java.text.DateFormat;
import java.util.Calendar;
import java.util.Date;

import static android.content.Context.KEYGUARD_SERVICE;

public class Utils {
    /**
     * 处理页面输入的工作时间
     * @param timeStr
     * @return
     */
    public static long processTime(String timeStr) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.HOUR_OF_DAY, Integer.parseInt(timeStr.split(":")[0]));
        calendar.set(Calendar.MINUTE, Integer.parseInt(timeStr.split(":")[1]));
        calendar.set(Calendar.SECOND, 0);
        return calendar.getTimeInMillis();
    }

    public static String df(long time){
        DateFormat df = DateFormat.getDateTimeInstance();
        return df.format(new Date(time));
    }

    //唤醒屏幕并解锁
    @SuppressLint("InvalidWakeLockTag")
    public static void wakeUpAndUnlock(Context context) {
        // 屏幕解锁
        KeyguardManager keyguardManager = (KeyguardManager) context.getSystemService(KEYGUARD_SERVICE);
        KeyguardManager.KeyguardLock keyguardLock = keyguardManager.newKeyguardLock("unLock");
        // 屏幕锁定
        keyguardLock.reenableKeyguard();
        keyguardLock.disableKeyguard(); // 解锁
        //获取PowerManager.WakeLock对象,后面的参数|表示同时传入两个值,最后的是LogCat里用的Tag
        PowerManager.WakeLock wl = ENV.pm.newWakeLock(PowerManager.FULL_WAKE_LOCK
                | PowerManager.ACQUIRE_CAUSES_WAKEUP
                | PowerManager.ON_AFTER_RELEASE | PowerManager.SCREEN_BRIGHT_WAKE_LOCK, "bright");
        //点亮屏幕
        wl.acquire();
        //释放
        wl.release();
    }

}
